<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/13
 * Time: 16:16
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class GroupMembersModel
{
    const TABLE = 'group_members';

    /**
     * @param array $data
     * @return mixed
     */
    public function lists(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('employee as e','e.id','=',self::TABLE.'.employeeId')
            ->where(function ($query) use ($data){
                if (!empty($data['status'])){
                    $query->where(self::TABLE.'.status',$data['status']);
                }
            })

            ->where(self::TABLE.'.groupId',$data['groupId'])
            ->get([self::TABLE.'.*','e.jobNumber','e.name as employeeName','e.status'])->toArray();
    }

    /**
     * @param array $data
     * @return bool
     */
    public function isInGroup(array $data)
    {
        $yearInfo = (new YearModel())->findYear(date('Y-m-d H:i:s'));
        if (!empty($yearInfo)){
            $data['startTime'] = $yearInfo['startTime'];
            $data['endTime'] = $yearInfo['endTime'];
        }

        $result = DB::table(self::TABLE)
            ->leftJoin('group as g','g.id','=',self::TABLE.'.groupId')
            ->where('g.status',1)
            ->where('g.projectId',$data['projectId'])
            ->where(self::TABLE.'.employeeId',$data['employeeId'])
            ->where(self::TABLE.'.status',1)
            ->whereBetween('g.createTime',[$data['startTime'],$data['endTime']])
            ->first();
        if (empty($result)){
            return false;
        }else{
            return get_object_vars($result);
        }
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(array $data){
        return DB::table(self::TABLE)->insert($data);
    }

    /**
     * @param array $where
     * @return mixed
     */
    public function delete(array $where)
    {
        return DB::table(self::TABLE)->where($where)->delete();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function info(array $data){
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function update(array $where, array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }
}